﻿Imports DaveSexton.Labs

Partial Public Class App
	Inherits Application

	Friend ReadOnly Property LabController As PhoneLabController
		Get
			Return controller
		End Get
	End Property

	Private controller As PhoneLabController
	Private rootFrame As PhoneApplicationFrame

	Public Sub New()
		' Standard Silverlight initialization
		InitializeComponent()

		' Phone-specific initialization
		InitializePhoneApplication()

		' Show graphics profiling information while debugging.
		If System.Diagnostics.Debugger.IsAttached Then
			' Display the current frame rate counters.
			Application.Current.Host.Settings.EnableFrameRateCounter = True

			' Show the areas of the app that are being redrawn in each frame.
			'Application.Current.Host.Settings.EnableRedrawRegions = True

			' Enable non-production analysis visualization mode, 
			' which shows areas of a page that are handed off to GPU with a colored overlay.
			'Application.Current.Host.Settings.EnableCacheVisualization = True


			' Disable the application idle detection by setting the UserIdleDetectionMode property of the
			' application's PhoneApplicationService object to Disabled.
			' Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run
			' and consume battery power when the user is not using the phone.
			PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled
		End If

	End Sub

	Private Sub Application_Launching(ByVal sender As Object, ByVal e As LaunchingEventArgs)
		Dim catalog = New Catalog()

		controller = New PhoneLabController(catalog)
	End Sub

	' Code to execute if a navigation fails
	Private Sub RootFrame_NavigationFailed(ByVal sender As Object, ByVal e As NavigationFailedEventArgs)
		If Diagnostics.Debugger.IsAttached Then
			' A navigation has failed; break into the debugger
			Diagnostics.Debugger.Break()
		End If
	End Sub

	Public Sub Application_UnhandledException(ByVal sender As Object, ByVal e As ApplicationUnhandledExceptionEventArgs) Handles Me.UnhandledException

		' Show graphics profiling information while debugging.
		If Diagnostics.Debugger.IsAttached Then
			Diagnostics.Debugger.Break()
		Else
			e.Handled = True
			MessageBox.Show(e.ExceptionObject.Message & Environment.NewLine & e.ExceptionObject.StackTrace,
			 "Error", MessageBoxButton.OK)
		End If
	End Sub

#Region "Phone application initialization"
	' Avoid double-initialization
	Private phoneApplicationInitialized As Boolean = False

	' Do not add any additional code to this method
	Private Sub InitializePhoneApplication()
		If phoneApplicationInitialized Then
			Return
		End If

		' Create the frame but don't set it as RootVisual yet; this allows the splash
		' screen to remain active until the application is ready to render.
		rootFrame = New PhoneApplicationFrame()
		AddHandler rootFrame.Navigated, AddressOf CompleteInitializePhoneApplication

		' Handle navigation failures
		AddHandler rootFrame.NavigationFailed, AddressOf RootFrame_NavigationFailed

		' Ensure we don't initialize again
		phoneApplicationInitialized = True
	End Sub

	' Do not add any additional code to this method
	Private Sub CompleteInitializePhoneApplication(ByVal sender As Object, ByVal e As NavigationEventArgs)
		' Set the root visual to allow the application to render
		If RootVisual IsNot rootFrame Then
			RootVisual = rootFrame
		End If

		' Remove this handler since it is no longer needed
		RemoveHandler rootFrame.Navigated, AddressOf CompleteInitializePhoneApplication
	End Sub
#End Region

End Class